package edu.uwm.RobberKnight.View;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Stack;

import edu.uwm.RobberKnight.Model.Knight;
import edu.uwm.RobberKnight.Model.Tile;

/*
 * Graphical representation of a tile.
 */
public class GuiTile {

    public final static int TILE_SIZE = 75;
    private Tile _tile;
    private int _x;
    private int _y;

    public GuiTile(Tile tile) {
        _tile = tile;
    }

    /*
     * Draw the face of the tile.
     */
    public void drawTileFace(Graphics g, boolean showKnights) {
        Color tileColor = getTileColor();
        Image img = Resources.getTileFaceImage(_tile.getTileType());

        g.setColor(tileColor);
        g.fillRect(_x, _y, TILE_SIZE, TILE_SIZE);
        if (img != null) {
            g.drawImage(img, _x, _y, TILE_SIZE, TILE_SIZE, null);
        }
        g.setColor(Color.white);
        g.drawRect(_x, _y, TILE_SIZE, TILE_SIZE);
        g.setColor(Color.black);
        if (img == null) 
	        g.drawString(_tile.getTileType().name(), _x + 2, _y + 20);
        if (showKnights) {
            Stack<Knight> knights = _tile.getKnights();
            if (knights != null) {
                for (int i = 0; i < knights.size(); i++) {
                    g.drawImage(Resources.getKnight(knights.get(i).getColor()), (_x + TILE_SIZE - 25 ), (_y + TILE_SIZE - 25) - (i * 7), null);
                }
            }
        }
    }

    /* 
     * Draw the back of the tile.
     */
    public void drawTileBack(Graphics g) {
        drawTileBack(g, 1);
    }

    /*
     * Draw the back of the tile scaled to a different size.
     */
    public void drawTileBack(Graphics g, float scale) {
        int size = (int) (TILE_SIZE * scale);
        g.drawImage(Resources.getTileBackImage(_tile.getPlayer().getColor(), _tile.getLetter()), _x, _y, size, size, null);
    }

    private Color getTileColor() {
        switch (_tile.getTileType()) {
            case CASTLE_FOREST:
                return new Color(139, 117, 0);
            case CASTLE_PLAIN:
                return new Color(255, 99, 71);
            case PLAIN:
                return new Color(205, 133, 63);
            case MOUNTAIN:
                return new Color(131, 139, 139);
            case LAKE:
                return new Color(126, 192, 238);
            case TOWN_FOREST:
                return new Color(0, 100, 0);
            case TOWN_PLAIN:
                return new Color(222, 184, 35);
            case VILLAGE_FOREST:
                return new Color(0, 255, 0);
            case VILLAGE_PLAIN:
                return new Color(227, 207, 87);
            default:
                return Color.yellow;
        }
    }

    public Tile getTile() {
        return _tile;
    }

    public int getX() {
        return _x;
    }

    public void setX(int x) {
        _x = x;
    }

    public int getY() {
        return _y;
    }

    public void setY(int y) {
        _y = y;
    }

    public boolean containsPoint(int x, int y) {
        return (x >= _x && x <= _x + TILE_SIZE) && (y >= _y && y <= _y + TILE_SIZE);
    }
}
